﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using AppTouchScreen.Helper;
using AppTouchScreen.Imaging;
using AppTouchScreen.Model;
using Microsoft.Win32;

namespace AppTouchScreen.ViewModels
{
    /// <summary>
    /// MVVM logic for MainWindow
    /// </summary>
    public class MainWindowViewModel : BaseViewModel
    {

        #region Data

        private DisplayCover _currentCover = null;

        private DisplayCover _selectedCover = null;

        private Visibility _editStoreVisibility = Visibility.Hidden;

        private Visibility _viewStoreVisibility = Visibility.Visible;

        #endregion //Data

        #region Constructor

        public MainWindowViewModel()
        {
            CurrentCover = DisplayCover.Load();
            StoreHelper.Instance.ChangeStoreState += new StoreHelper.ChangeStoreStateEventHandler(StoreHelper_ChangeStoreState);
            //Show controls for edit if design time
            if (GetIsInDesignMode())
            {
                StoreHelper_ChangeStoreState(StoreState.Edit);
            }
            //Save on exit
            App.Current.Exit += ((x, y) =>
                                     {
                                         CurrentCover.Root.Save();
                                     });
        }

        #endregion //Constructor

        #region Methods

        /// <summary>
        /// Change current element for view
        /// </summary>
        /// <param name="currentCover"></param>
        private void ChangeCurrentCover(DisplayCover currentCover)
        {
            if (currentCover != null)
            {
                _currentCover = currentCover;
                OnPropertyChanged(() => CurrentCover);
                SelectedCover = _currentCover;
            }
        }

        /// <summary>
        /// Change store StoreState for all app
        /// </summary>
        /// <param name="state"></param>
        private void StoreHelper_ChangeStoreState(StoreState state)
        {
            if (state == StoreState.View)
            {
                EditStoreVisibility = Visibility.Hidden;
                ViewStoreVisibility = Visibility.Visible;
            }
            else if(state == StoreState.Edit)
            {
                EditStoreVisibility = Visibility.Visible;
                ViewStoreVisibility = Visibility.Hidden;
            }
            else
            {
                throw new Exception("Not found enum: " + state.ToString());
            }
        }

        #endregion //Methods

        #region Notify property

        /// <summary>
        /// DisplayCover that is parent for elements in the listview
        /// </summary>
        public DisplayCover CurrentCover
        {
            get { return _currentCover; }
            set
            {
                ChangeCurrentCover(value);
            }
        }

        /// <summary>
        /// Selected DisplayCover
        /// </summary>
        public DisplayCover SelectedCover
        {
            get { return _selectedCover; }
            set
            {
                _selectedCover = value;
                OnPropertyChanged(() => SelectedCover);
            }
        }

        public Visibility EditStoreVisibility
        {
            get { return _editStoreVisibility; }
            set
            {
                _editStoreVisibility = value;
                OnPropertyChanged(() => EditStoreVisibility);
            }
        }

        public Visibility ViewStoreVisibility
        {
            get { return _viewStoreVisibility; }
            set
            {
                _viewStoreVisibility = value;
                OnPropertyChanged(() => ViewStoreVisibility);
            }
        }

        #endregion //INotifyProperty

        #region Commands

        public ICommand OpenCategoryCommand { get { return new BaseCommand(OpenCategory, CanOpenCategory); } }

        public ICommand ChangePictureCommand { get { return new BaseCommand(ChangePicture); } }

        public ICommand AddCategoryCommand { get { return new BaseCommand(AddCategory); } }

        public ICommand AddProductCommand { get { return new BaseCommand(AddProduct); } }

        public ICommand DeleteDisplayCoverCommand { get { return new BaseCommand(DeleteDisplayCover); } }

        public ICommand ChangeStoreStateCommand { get { return new BaseCommand(ChangeStoreState); } }

        /// <summary>
        /// Go to selected category
        /// </summary>
        /// <param name="parametr"></param>
        private void OpenCategory(object parametr)
        {
            if (parametr != null)
            {
                if (parametr is DisplayCover)
                {
                    var dc = parametr as DisplayCover;
                    if (dc.IsCategory)
                    {
                        CurrentCover = dc;
                    }
                }
            }
        }

        /// <summary>
        /// Can go to selected category
        /// </summary>
        /// <param name="parametr"></param>
        /// <returns></returns>
        public bool CanOpenCategory(object parametr)
        {
            bool result = false;

            if (parametr != null)
            {
                if (parametr is DisplayCover)
                {
                    var dc = parametr as DisplayCover;
                    if (dc.IsCategory)
                    {
                        result = true;
                    }
                }
            }
            return result;
        }

        private void ChangePicture()
        {
            var ofd = new OpenFileDialog(); 
            if (ofd.ShowDialog() == true)
            {
                if (SelectedCover != null)
                {
                    SelectedCover.Picture = ImageInfo.CreateImageInfo(ofd.FileName);

                    OnPropertyChanged(() => SelectedCover);
                    OnPropertyChanged(() => CurrentCover);
                }
            }
        }

        /// <summary>
        /// Add new category and go into
        /// </summary>
        private void AddCategory()
        {
            CurrentCover = CurrentCover.AddCategory();
            CurrentCover.Name = "New category";
            CurrentCover.Description = "The description of new category";
        }

        /// <summary>
        /// Add new product and select this product
        /// </summary>
        private void AddProduct()
        {
            SelectedCover = CurrentCover.AddProduct();
            OnPropertyChanged(() => SelectedCover);
            OnPropertyChanged(() => CurrentCover);
        }

        /// <summary>
        /// delete current display cover
        /// </summary>
        private void DeleteDisplayCover()
        {
            if (SelectedCover.Parent != null)
            {
                DisplayCover cover = SelectedCover;
                SelectedCover = cover.Parent;
                cover.Delete();               
                OnPropertyChanged(() => CurrentCover);
            }
        }

        /// <summary>
        /// Change storestate of this app
        /// </summary>
        private void ChangeStoreState()
        {
            switch (StoreHelper.Instance.StoreState)
            {
                case StoreState.View:
                    StoreHelper.Instance.StoreState = StoreState.Edit;
                    break;
                case StoreState.Edit:
                    StoreHelper.Instance.StoreState = StoreState.View;
                    break;
                default:
                    break;
            }
        }

        #endregion //commands

    }
}
